﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABC_SA_BLV;
using ABC_SA_DAL;
using ABC_SA_BL;
using ABC_SA_DM;

/*
* 0 - transporte
* 1 - hospedaje
* 2 - alimentacion
* 3 - actividades
* 4 - entradas
*/
namespace ABC_SA
{
    public partial class ServicioForm : Form
    {
        public ServicioForm()
        {
            InitializeComponent();
            textBox1.Focus();
            comboBox1.Items.Add("0 - transporte");
            comboBox1.Items.Add("1 - hospedaje");
            comboBox1.Items.Add("2 - alimentacion");
            comboBox1.Items.Add("3 - actividades");
            comboBox1.Items.Add("4 - entradas");
        }

        private Int32 idServ;
        Servicio serv = new Servicio();

        public void consultar() {
            Servicios_BL serv = new Servicios_BL();
            dataserv.DataSource = serv.ConsultarServicios();
            idServ = 0;
            label9.Text = "Id de Servicio: " + idServ;
            label10.Text = "...";
            textBox3.Clear();
            textBox5.Clear();
            comboBox2.Items.Clear();
            comboBox2.ResetText();
        }
        public void setCampos(int x) {
            switch (x) { 
                case 0:
                    label5.Text = "datos incorrectos favor revisar";
                    break;
                case 1:
                    comboBox1.SelectedIndex = -1;
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox1.Focus();
                    label5.Text = "datos ingresados correctamente";
                    break;
            }
        }
        public void actualizar(bool cont) { 
            if (cont == true) {
                textBox3.Clear();
                textBox5.Clear();
                comboBox2.Items.Clear();
                comboBox2.ResetText();
                idServ = 0;
                consultar();
                label10.Text = "Datos actualizados con exito";
            } else {
                label10.Text = "No se pudieron actualizar los datos";
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){}
        private void label1_Click(object sender, EventArgs e){}
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string n;
                int c;
                double m;
                int x;

                c = comboBox1.SelectedIndex;
                n = textBox1.Text;
                if (textBox2.Text == "")
                {
                    m = 0;
                }
                else
                {
                    m = Convert.ToDouble(textBox2.Text);
                }

                Servicios_BLV servicio = new Servicios_BLV();

            
                x = servicio.CrearServicio(n, c, m);
                setCampos(x);
            }
            catch (Exception){
                MessageBox.Show("dato del monto invalido");
                textBox2.Clear();
                textBox2.Focus();
            }              
        }
        private void tabPage2_Click(object sender, EventArgs e){}
        private void label6_Click(object sender, EventArgs e){}
        private void button3_Click(object sender, EventArgs e){
            consultar();
        }
        private void dataserv_Click(object sender, EventArgs e)
        {
            try
            {
                textBox3.Text = Convert.ToString(dataserv.CurrentRow.Cells[1].Value);
                textBox5.Text = Convert.ToString(dataserv.CurrentRow.Cells[3].Value);
                if (comboBox2.Items.Count == 0)
                {
                    comboBox2.Items.Add("0 - transporte");
                    comboBox2.Items.Add("1 - hospedaje");
                    comboBox2.Items.Add("2 - alimentacion");
                    comboBox2.Items.Add("3 - actividades");
                    comboBox2.Items.Add("4 - entradas");
                }
                comboBox2.SelectedIndex = Convert.ToInt32(dataserv.CurrentRow.Cells[2].Value);
                idServ = Convert.ToInt32(dataserv.CurrentRow.Cells[0].Value);

                serv.IdServicio = idServ;
                serv.Monto = Convert.ToDouble(dataserv.CurrentRow.Cells[3].Value);
                serv.Nombre = Convert.ToString(dataserv.CurrentRow.Cells[1].Value);
                serv.Categoria = Convert.ToInt32(dataserv.CurrentRow.Cells[2].Value);

                label9.Text = "Id de Servicio: "+idServ;
                label10.Text = "...";
            }
            catch (Exception ) { 
            
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string n = textBox3.Text;
                double m = Convert.ToDouble(textBox5.Text);
                Int32 c = Convert.ToInt32(comboBox2.SelectedIndex);

                Servicios_BLV servicio = new Servicios_BLV();
                bool cont = servicio.ModificarServicio(serv, n, c, m);

                actualizar(cont);

                
            }
            catch (Exception) { 
            
            }
        }

        private void btnatras_Click(object sender, EventArgs e)
        {
            AdminForm admin = new AdminForm();
            this.Dispose();
            admin.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Int32 id = idServ;
            Servicios_BL serv = new Servicios_BL();

            bool cont = serv.EliminarServicios(idServ);

            actualizar(cont);
        }
    }
}
